package com.qf.java2107.yb.hotel.controller.foodtype;

import com.alibaba.fastjson.JSON;
import com.qf.java2107.yb.hotel.common.ResultVO;
import com.qf.java2107.yb.hotel.constant.MethodConstant;
import com.qf.java2107.yb.hotel.controller.BaseServlet;
import com.qf.java2107.yb.hotel.entity.BeanFactory;
import com.qf.java2107.yb.hotel.pojo.FoodType;
import com.qf.java2107.yb.hotel.service.FoodTypeService;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @author yb
 * @version 1.0
 * @date 2021-10-28
 **/
@WebServlet("/foodtype")
public class FoodTypeController extends BaseServlet {

    private FoodTypeService foodTypeService = (FoodTypeService) BeanFactory.getBean("foodTypeService");

    /**
     * 保存
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String save(HttpServletRequest request, HttpServletResponse response) throws Exception {

        String typeName = request.getParameter("typeName");
        FoodType foodType = new FoodType();
        foodType.setTypeName(typeName.trim());
        ResultVO vo = foodTypeService.save(foodType);

        if(vo.getSuccess()) {
            return MethodConstant.REDIRECT_PREFIX + request.getContextPath() + "/foodtype?" + MethodConstant.METHOD + "=search";
        }
        return (String)vo.getData();

    }

    /**
     * 删除
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String delete(HttpServletRequest request, HttpServletResponse response) throws Exception {

        String typeId = request.getParameter("typeId");
        ResultVO vo = foodTypeService.deleteById(Integer.parseInt(typeId));
        if(vo.getSuccess()) {
            return "redirect:" + request.getContextPath() + "/foodtype?" + MethodConstant.METHOD + "=search";
        }
        return (String)vo.getData();

    }

    /**
     * 跳转修改页面
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String updateui(HttpServletRequest request, HttpServletResponse response) throws Exception {

        String typeId = request.getParameter("typeId");
        ResultVO vo = foodTypeService.findById(Integer.parseInt(typeId));
        if(vo.getSuccess()) {
            request.setAttribute("foodType", vo.getData());
            String backendDetailFolder = (String) getServletContext().getAttribute("backendDetailFolder");
            return MethodConstant.FORWARD_PREFIX + backendDetailFolder + "/foodtype/foodtype-update.jsp";
        }
        return (String)vo.getData();

    }

    /**
     * 修改
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String update(HttpServletRequest request, HttpServletResponse response) throws Exception {

        //如何快速封装前台请求参数到一个Java实体数据
        Map<String, String[]> parameterMap = request.getParameterMap();
        //实体模型
        FoodType foodType = new FoodType();
        //把map中的数据封装到一个实体模型中。有一个要求，map的key必须和Java实体属性名相同
        BeanUtils.populate(foodType, parameterMap);
        ResultVO vo = foodTypeService.update(foodType);
        if(vo.getSuccess()) {
            return "redirect:" + request.getContextPath() + "/foodtype?" + MethodConstant.METHOD + "=search";
        }
        return (String)vo.getData();
    }

    /**
     * 查询
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String search(HttpServletRequest request, HttpServletResponse response) throws Exception {

        //获取请求参数
        String keyword = request.getParameter("keyword");
        ResultVO vo = foodTypeService.findLikeTypeName(keyword);
        if(vo.getSuccess()) {
            //条件回显
            request.setAttribute("keyword", keyword);
            request.setAttribute("types", vo.getData());
            return "forward:" + request.getContextPath() + "/backend/detail/foodtype/foodtype-list.jsp";
        }
        return (String) vo.getData();

    }

    public String nameExists(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String typeName = request.getParameter("typeName");
        ResultVO vo = foodTypeService.findByTypeName(typeName);
        return JSON.toJSONString(vo);
    }

}
